package com.yuncheng.spcyApi.selectXzxkApi.service;

import com.alibaba.fastjson.JSONObject;
import com.yuncheng.spcyApi.selectXzxkApi.vo.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.spcyApi.mapper.XzXkMapper;
import com.yuncheng.spcyApi.selectXzxkApi.constant.XzXkConstant;
import com.yuncheng.spcyApi.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component(value = "hzpXzxkService")
public class HzpXzxkService {
    private static final Logger log = LoggerFactory.getLogger(YpscXzxkService.class);

    @Resource
    @Lazy
    private XzXkMapper xzXkMapper;

    /**
     *  sqsxbm (003001) / 化妆品生产许可证核发
     */
    public Map hzpScxkzHf(String sqsxid) {
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业主要人员-其他人员
        resultMap.putAll(this.qyryListMap(sqsxid));

        // 企业项目资产、产品品种
        List<XzspXmzcVo> xmzcList = xzXkMapper.selectXmzc(sqsxid);
        if (CollectionUtils.isNotEmpty(xmzcList)){
            resultMap.put("xmzc", xmzcList.get(0));
        }else {
            resultMap.put("xmzc", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        if (CollectionUtils.isNotEmpty(sbqkList)){
            List<XzspSbVo> scsbList = sbqkList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getSblx())
                            && f.getSblx().equals("生产设备"))
                    .collect(Collectors.toList());

            List<XzspSbVo> jysbList = sbqkList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getSblx())
                            && f.getSblx().equals("检验设备"))
                    .collect(Collectors.toList());
            resultMap.put("scsbList", scsbList);
            resultMap.put("jysbList", jysbList);
        }else {
            resultMap.put("scsbList", new ArrayList<>(1));
            resultMap.put("jysbList", new ArrayList<>(1));
        }


        return resultMap;
    }

    /**
     *  sqsxbm (003007) / 化妆品生产许可证注销
     */
    public Map hzpScxkzZx(String sqsxid) {
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业人员-主要人员
        List<QyRyVo> qyryList = xzXkMapper.selectQyRyList(sqsxid, null, true);
        resultMap.put("primaryQyryList", qyryList);

        // 企业项目资产、产品品种
        List<XzspXmzcVo> xmzcList = xzXkMapper.selectXmzc(sqsxid);
        if (CollectionUtils.isNotEmpty(xmzcList)){
            resultMap.put("xmzc", xmzcList.get(0));
        }else {
            resultMap.put("xmzc", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }


        return resultMap;
    }

    /**
     *  sqsxbm (003004) / 化妆品生产许可证许可事项变更
     */
    public Map hzpScxkzXksxBg(String sqsxid) {
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业项目资产、产品品种
        List<XzspXmzcVo> xmzcList = xzXkMapper.selectXmzc(sqsxid);
        if (CollectionUtils.isNotEmpty(xmzcList)){
            resultMap.put("xmzc", xmzcList.get(0));
        }else {
            resultMap.put("xmzc", new JSONObject());
        }

        // 变更前、变更后的 内容;
        resultMap.put("lsxx" ,this.lsxx(sqsxid));

        return resultMap;
    }

    /**
     *  sqsxbm (003009) / 国产非特殊用途化妆品备案
     */
    public Map gcFtsytHzpBa(String sqsxid) {
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 国产非特殊用途化妆品
        List<XzspGcftsythzpVo> ftsHzpList = xzXkMapper.hzpSelectGcftsytHzp(sqsxid);
        if (CollectionUtils.isNotEmpty(ftsHzpList)){
            resultMap.put("ftsHzp", ftsHzpList.get(0));
        }else {
            resultMap.put("ftsHzp", new JSONObject());
        }

        return resultMap;
    }

    /**
     *  sqsxbm (003003) / 化妆品生产许可证登记事项变更
     */
    public Map hzpScxkzDjsxBg(String sqsxid) {
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业项目资产、产品品种
        List<XzspXmzcVo> xmzcList = xzXkMapper.selectXmzc(sqsxid);
        if (CollectionUtils.isNotEmpty(xmzcList)){
            resultMap.put("xmzc", xmzcList.get(0));
        }else {
            resultMap.put("xmzc", new JSONObject());
        }

        // 变更前、变更后的 内容;
        resultMap.put("lsxx" ,this.lsxx(sqsxid));


        return resultMap;
    }

    /**
     *  sqsxbm (003005) / 化妆品生产许可报告事项变更
     */
    public Map hzpScxkBgsxBg(String sqsxid) {
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业项目资产、产品品种
        List<XzspXmzcVo> xmzcList = xzXkMapper.selectXmzc(sqsxid);
        if (CollectionUtils.isNotEmpty(xmzcList)){
            resultMap.put("xmzc", xmzcList.get(0));
        }else {
            resultMap.put("xmzc", new JSONObject());
        }

        // 变更前、变更后的 内容;
        resultMap.put("lsxx" ,this.lsxx(sqsxid));

        return resultMap;
    }

    /**
     *  sqsxbm (003002) / 化妆品生产许可证延续 (待确认) (待完善)
     */
    public Map hzpScxkzyX(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业 主要人员 / 其他人员
        resultMap.putAll(this.qyryListMap(sqsxid));

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        if (CollectionUtils.isNotEmpty(sbqkList)){
            List<XzspSbVo> scsbList = sbqkList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getSblx())
                            && f.getSblx().equals("生产设备"))
                    .collect(Collectors.toList());

            List<XzspSbVo> jysbList = sbqkList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getSblx())
                            && f.getSblx().equals("检验设备"))
                    .collect(Collectors.toList());
            resultMap.put("scsbList", scsbList);
            resultMap.put("jysbList", jysbList);
        }else {
            resultMap.put("scsbList", new ArrayList<>(1));
            resultMap.put("jysbList", new ArrayList<>(1));
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 企业项目资产、产品品种
        List<XzspXmzcVo> xmzcList = xzXkMapper.selectXmzc(sqsxid);
        if (CollectionUtils.isNotEmpty(xmzcList)){
            resultMap.put("xmzc", xmzcList.get(0));
        }else {
            resultMap.put("xmzc", new JSONObject());
        }

        // 变更前、变更后的 内容;
        resultMap.put("lsxx" ,this.lsxx(sqsxid));

        return resultMap;
    }

    /**
     *  sqsxbm (003006) / 化妆品生产许可证补发 (待确认) (待完善)
     */
    public Map hzpScxkzBf(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     *  sqsxbm (003008) / 化妆品生产许可延续（告知承诺）审批 (待确认) (待完善)
     */
    public Map hzpScxkYxGzcnSp(String sqsxid){
        Map resultMap = new HashMap();

        return resultMap;
    }

    /**
     * 企业人员-主要人员 / 其他人员
     */
    public Map qyryListMap(String sqsxid){
        Map resultMap = new HashMap();

        // 企业人员-主要人员
        List<QyRyVo> qyryList = xzXkMapper.selectQyRyList(sqsxid, null, true);
        resultMap.put("primaryQyryList", qyryList);

        // 企业其他人员
        List<QyRyVo> qtQyryList = xzXkMapper.selectQyRyList(sqsxid, XzXkConstant.Fzrlx_Type_00, false);
        resultMap.put("qtQyryList", qtQyryList);

        return resultMap;
    }

    /**
     * 查询 - 历史表 - 历史记录 - 返回 -变更前，变更后的信息
     */
    public JSONObject lsxx(String sqsxid){
        String ccdx = "";
        Map<String, Object> lsbMap = xzXkMapper.searchLsb(sqsxid);
        if (lsbMap != null){
            Map<String, Object> lsxxMap = xzXkMapper.searchLsjl(lsbMap.get("ID").toString());
            if (lsxxMap != null){
                try {
                    ccdx =  MapUtils.ClobToString((Clob)lsxxMap.get("CCDX"));
                } catch (SQLException e) {
                    log.error("SQLException[]: {}", e.getMessage());
                } catch (IOException e) {
                    log.error("SQLException[]: {}", e.getMessage());
                }
            }
        }

        if (StringUtils.isNotBlank(ccdx)){
            JSONObject jsonObject = JSONObject.parseObject(ccdx);
            return jsonObject;
        }else {
            return new JSONObject();
        }
    }
}
